
/**
 *@author Airlangga
 */
public class MainUI {

    private ProjModel pm;
    private GridView gv;

    public MainUI() {
        pm = new ProjModel();
        gv = new GridView(this.pm);
    }

    public void showMenu() {
        /** shows user menu and get user input */
        while (true) {

            System.out.print(" === MENU ===\n"
                    + " 1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n"
                    + " 2. Set Number of vertical lanes  (V-street) [min 1, max 4]\n"
                    + " 3. Set Probability of a car entering H-street [min 0, max 1]\n"
                    + " 4. Set Probability of a car entering V-street [min 0, max 1]\n"
                    + " 5. Run one simulation cycle\n"
                    + " 6. Set and run number of simulation cycles [min 1, max 10]\n"
                    + " ");
            String userChoice = GetUserInput.getUserChoice("Enter your choice>");
            int userChoiceInt = Integer.parseInt(userChoice);
            System.out.println("After int conversion" + userChoiceInt);

            switch (userChoiceInt) {
                case 1:
                    System.out.println("Menu 1");
                    setNumHorLanes();
                    break;
                case 2:
                    System.out.println("Menu 2");
                    setNumVerLanes();
                    break;
                case 3:
                    System.out.println("Menu 3");
                    setHorProb();
                    break;
                case 4:
                    System.out.println("Menu 4");
                    setVerProb();
                    break;
                case 5:
                    System.out.println("Menu 5");
                    gv.initGrid('x', 1);
                    //gv.showGrid();
                    break;
                case 6:
                    System.out.println("Menu 6");
                    setCycle();
                    break;
                default:
                    System.out.println("Error in entering menu choice");
                    break;
            }

        }
    }

    /** set number of horizontal lane from user input */
    public void setNumHorLanes() {
        String userChoice = GetUserInput.getUserChoice("Enter number of horiz lanes : ");
        int userChoiceInt = Integer.parseInt(userChoice);
        int lowerLimit = 1;
        int upperLimit = 3;
        if (userChoiceInt > upperLimit || userChoiceInt < lowerLimit) {
            System.out.println("Error in entering horiz lanes");
            System.out.println("Please enter number above 1 and below 3. ");
            showMenu();
        } else { //if user enter the correct input
            pm.setHorLanes(userChoiceInt);
            //let user set the number
        }

    }

    /** set number of vertical lane from user input */
    public void setNumVerLanes() {
        String userChoice = GetUserInput.getUserChoice("Enter number of vert lanes : ");
        int userChoiceInt = Integer.parseInt(userChoice);
        int lowerLimit = 1;
        int upperLimit = 4;
        if (userChoiceInt > upperLimit || userChoiceInt < lowerLimit) {
            System.out.println("Error in entering horiz lanes");
            System.out.println("Please enter number above 1 and below 3. ");
            showMenu();
        } else { //if user enter the correct input
            pm.setVerLanes(userChoiceInt);
            //let user set the number
        }

    }

    /** set horizontal probability from user input */
    public void setHorProb() {
        String userChoice = GetUserInput.getUserChoice("Enter number of Horizontal prob : ");
        double userChoiceDouble = Double.parseDouble(userChoice);
        int lowerLimit = 0;
        int upperLimit = 1;
        if (userChoiceDouble > upperLimit || userChoiceDouble < lowerLimit) {
            System.out.println("Error in entering probability");
            System.out.println("Please enter number above 0 and below 1. ");
            showMenu();
        } else { //if user enter the correct input
            pm.setHorProb(userChoiceDouble);
            //let user set probability
        }

    }

    /** set vertical  probability from user input */
    public void setVerProb() {
        String userChoice = GetUserInput.getUserChoice("Enter number of Vertical prob : ");
        double userChoiceDouble = Double.parseDouble(userChoice);
        int lowerLimit = 0;
        int upperLimit = 1;
        if (userChoiceDouble > upperLimit || userChoiceDouble < lowerLimit) {
            System.out.println("Error in entering probability");
            System.out.println("Please enter number above 0 and below 1. ");
            showMenu();
        } else { //if user enter the correct input
            pm.setVerProb(userChoiceDouble);
            //let user set the probability
        }

    }

    public void setCycle() {
        String userChoice = GetUserInput.getUserChoice("Enter number of cycle(s) (1-10) : ");
        int userChoiceInt = Integer.parseInt(userChoice);
        int lowerLimit = 0;
        int upperLimit = 10;
        if (userChoiceInt > upperLimit || userChoiceInt < lowerLimit) {
            System.out.println("Error in entering number of cycle(s)");
            System.out.println("Please enter number above 0 and below 10. ");
            showMenu();
        } else {
            //for (int i = 0; i < userChoiceInt; i++){
            gv.initGrid('#', pm.getCycle());
            //gv.showGrid();

        }




    }

    /** shows horizontal lanes */
    public int showHorLanes() {
        return pm.getHorLanes();
    }

    /** shows vertical lanes */
    public int showVerLanes() {
        return pm.getVerLanes();
    }
}
